﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HRM.Entity;
using HRM.BIZ;
using HRM.Utility;
using System.Collections.Generic;

namespace WebUI.Pages
{
    public partial class EmployeeContractPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadEmployeeContract();
                LoadContractType();
            }
        }

        protected void LoadEmployeeContract()
        {
            int id = Request.QueryString["id"].ToInt32();
            EmployeeContractBiz empBiz = new EmployeeContractBiz();
            var contracts = empBiz.GetAllByEmpID(id);
            ViewState["DataSource"] = contracts;
            grvEmployeeContract.DataSource = contracts;
            grvEmployeeContract.DataBind();

            var emp = new EmployeeBiz().GetByID(id);
            if (emp != null)
            {
                ucEmpNavigate.RecordID = id;
                ucEmpNavigate.EmployeeName = emp.Name;
            }
        }

        protected void LoadContractType()
        {
            List<ContractType> contractTypes = new ContractTypeBiz().GetAll();
            ddlContract.DataSource = contractTypes;
            ddlContract.DataTextField = "Name";
            ddlContract.DataValueField = "ID";
            ddlContract.DataBind();
        }

        protected void btnAction_Click(object sender, EventArgs e)
        {
            switch (btnAction.CommandArgument)
            {
                case "Add":
                    {
                        EmployeeContractBiz empBiz = new EmployeeContractBiz();
                        EmployeeContract empContract = new EmployeeContract();
                        empContract.EmpID = Request.QueryString["id"].ToInt32();
                        empContract.ContractTypeID = ddlContract.SelectedValue.ToInt32();
                        empContract.From = txtValidFrom.Text.ToDateTime("dd/MM/yyyy");
                        empContract.To = txtValidTo.Text.ToDateTime("dd/MM/yyyy");
                        empBiz.Add(empContract);

                        divAddEdit.Style.Add("display", "none");
                        LoadEmployeeContract();
                        (Master as DefaultMasterPage).RunStartupScript("displayMessage('Thêm thành công.')");
                        break;
                    }
                case "Update":
                    {
                        EmployeeContractBiz empBiz = new EmployeeContractBiz();
                        EmployeeContract item = empBiz.GetByID(hdfID.Value.ToInt32());
                        if (item != null)
                        {
                            item.ContractTypeID = ddlContract.SelectedValue.ToInt32();
                            item.From = txtValidFrom.Text.ToDateTime("dd/MM/yyyy");
                            item.To = txtValidTo.Text.ToDateTime("dd/MM/yyyy");

                            empBiz.Update(item);
                            divAddEdit.Style.Add("display", "none");
                            LoadEmployeeContract();
                            (Master as DefaultMasterPage).RunStartupScript("displayMessage('Cập nhật thành công.');");
                            
                        }
                        break;
                    }
            }
        }

        protected void grvEmployeeContract_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int contractTypeID = e.Row.Cells[2].Text.ToInt32();
                ContractType contractType = new ContractTypeBiz().GetByID(contractTypeID);
                if (contractType != null)
                {
                    e.Row.Cells[2].Text = contractType.Name;
                }

                if(e.Row.Cells[3].Text.ToDateTime("dd/MM/yyyy").Equals(new DateTime(1900,1,1)))
                {
                    e.Row.Cells[3].Text = "";
                }

                if (e.Row.Cells[4].Text.ToDateTime("dd/MM/yyyy").Equals(new DateTime(1900, 1, 1)))
                {
                    e.Row.Cells[4].Text = "";
                }
            }
            else if (e.Row.RowType == DataControlRowType.Header)
            {
                CheckBox chk = e.Row.FindControl("chkAll") as CheckBox;
                if (chk != null)
                {
                    chk.Attributes.Add("onclick", string.Format("selectAll('{0}')", chk.ClientID));
                }
            }
        }

        protected void grvEmployeeContract_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                CheckBox chkAll = new CheckBox();
                chkAll.ID = "chkAll";
                e.Row.Cells[0].Controls.Add(chkAll);
            }
        }

        protected void grvEmployeeContract_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "EditContract":
                    {
                        EmployeeContractBiz empBiz = new EmployeeContractBiz();
                        EmployeeContract item = empBiz.GetByID(e.CommandArgument.ToInt32());
                        if (item != null)
                        {
                            ddlContract.SelectedValue = item.ContractTypeID.ToString();
                            txtValidFrom.Text = item.From.Equals(new DateTime(1900, 1, 1)) ? "" : item.From.ToString("dd/MM/yyyy");
                            txtValidTo.Text = item.To.Equals(new DateTime(1900, 1, 1)) ? "" : item.To.ToString("dd/MM/yyyy");
                            hdfID.Value = item.ID.ToString();
                            btnAction.CommandArgument = "Update";
                            btnAction.Text = "Lưu";

                            divAddEdit.Style.Add("display", "block");
                        }
                        break;
                    }
                case "DeleteContract":
                    {
                        EmployeeContractBiz empBiz = new EmployeeContractBiz();
                        empBiz.Delete(e.CommandArgument.ToInt32());
                        LoadEmployeeContract();
                        break;
                    }      
            }
        }

        protected void grvEmployeeContract_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            if (ViewState["DataSource"] != null)
            {
                grvEmployeeContract.PageIndex = e.NewPageIndex;
                grvEmployeeContract.DataSource = ViewState["DataSource"];
                grvEmployeeContract.DataBind();
            }
        }

        protected void lnkAdd_Click(object sender, EventArgs e)
        {
            btnAction.CommandArgument = "Add";
            btnAction.Text = "Thêm";
            hdfID.Value = "";
            txtValidTo.Text = "";
            txtValidFrom.Text = "";

            divAddEdit.Style.Add("display", "block");
        }

    }
}
